package org.x4ut15m.xwebshell.explorer.file;

import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.Objects;

/**
 * 文件对象
 */
public class FileItem {
    private String fileName;
    private String fileType;
    private String absolutePath;
    private LinkedList<FileItem> subFiles;
    private boolean isFile;
    private boolean isDirectory;
    private boolean readable;
    private boolean writable;
    private boolean executable;
    private long size;
    private long lastModified;
    private Gson gson = new Gson();

    public String getFileName() {
        return fileName;
    }

    public boolean isExecutable() {
        return executable;
    }

    public boolean isDirectory() {
        return isDirectory;
    }

    public boolean isReadable() {
        return readable;
    }

    public boolean isFile() {
        return isFile;
    }

    public boolean isWritable() {
        return writable;
    }

    public void setExecutable(boolean executable) {
        this.executable = executable;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setReadable(boolean readable) {
        this.readable = readable;
    }

    public void setWritable(boolean writable) {
        this.writable = writable;
    }

    public LinkedList<FileItem> getSubFiles() {
        return subFiles;
    }

    public String getLastModified() {
        Date date = new Date(lastModified);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = formatter.format(date);
        return formattedDate;
    }

    public FileItem(String fileName, String fileType, String absolutePath) {
        this.fileName = fileName;
        this.fileType = fileType;
        this.absolutePath = absolutePath;
    }

    /**
     * 获取绝对路径
     */
    public String getAbsolutePath() {
        return absolutePath;
    }

    public boolean isHasSubDirectory() {
        return subFiles != null && !subFiles.isEmpty();
    }

    @Override
    public String toString() {
        if ("".equals(fileName)) {
            return absolutePath;
        }
        return fileName;
    }

    public String toJson() {
        return gson.toJson(this);
    }

    public String ls() {
        String result = "";
        if (getSubFiles() != null && !getSubFiles().isEmpty()) {
            for (FileItem subFile : getSubFiles()) {
                result += subFile.ls();
            }
        } else {
            result = String.format("%s%s%s%s   %d   %s   %s\n",
                    getFileType(),
                    isReadable() ? "r" : "-",
                    isWritable() ? "w" : "-",
                    isExecutable() ? "x" : "-",
                    getSize(),
                    getLastModified(),
                    getFileName());
        }


        return result;
    }

    public void setFileType(String fileType) {
        this.fileType = fileType;
    }

    public String getFileType() {
        return fileType;
    }

    public long getSize() {
        return size;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FileItem that = (FileItem) o;
        return Objects.equals(fileName, that.fileName) &&
                Objects.equals(fileType, that.fileType) && Objects.equals(absolutePath, that.absolutePath);
    }
}
